package com.yssoft.controller.app.supplier;

import com.yssoft.entity.core.TSupplier;
import com.yssoft.entity.core.TenterpriseLicence2;
import com.yssoft.service.core.DdDetailService;
import com.yssoft.service.core.SupplierService;
import com.yssoft.utils.AppUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * @author 戈超
 * @time 2018/4/29 上午 10:57
 * @  供应商管理控制器
 */
@CrossOrigin//解决跨域session问题
@Controller
@RequestMapping(value = "/app/supplier")
public class SupplierController {

	private static Logger logger=Logger.getLogger(SupplierService.class);
	
	@Resource
	private SupplierService supplierService;
	@Resource
	private DdDetailService ddDetailService;
	
	/**
     * 跳转到供应商信息页面
     * @return
     */
    @RequestMapping("/supplierList")
    public String toEnterprise(){
        return "core/supplier/supplierList";
    }
    
    /**
     * 查询供应商列表
     * @param page
     * @param limit
     * @param tSupplier
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "list", method = {RequestMethod.POST,RequestMethod.GET})
    public Object querySupplierList(Integer page, Integer limit, TSupplier tSupplier, HttpServletRequest request){
    	String msg = "供应商查询";
    	try{
    		List<Map<String,Object>> pageList = supplierService.querySupplierList(page, limit, tSupplier, request);
        	return AppUtil.returnListJson("1", msg+"成功!", pageList);
    	} catch (Exception e) {
    		logger.info(msg+"失败："+e.getMessage());
    		return AppUtil.returnListJson("0", msg+"失败!", null);
		}
    	
    }
    
    /**
     * 供应商查看
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "findOne", method = {RequestMethod.POST,RequestMethod.GET})
    public Object findOne(Integer id){
    	String msg = "供应商查看";
    	try{
    		Map<String,Object> tSupplier = supplierService.findOne(id);
    		return AppUtil.returnMapJson("1", msg+"成功!", tSupplier);
    	} catch (Exception e) {
    		logger.info(msg+"失败："+e.getMessage());
    		return AppUtil.returnMapJson("0", msg+"失败!", null);
		}
    }
    
    /**
     * 供应商类型查询
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "queryClify", method = {RequestMethod.POST,RequestMethod.GET})
    public Object queryClify(){
    	String msg = "供应商类型查询";
    	try{
    		Map<String,Object> param = new HashMap<>();
    		param.put("classify", "supplierClassify");
    		List<Map<String,Object>> tSupplier = ddDetailService.queryDDTree(param);
    		return AppUtil.returnListJson("1", msg+"成功!", tSupplier);
    	} catch (Exception e) {
    		logger.info(msg+"失败："+e.getMessage());
    		return AppUtil.returnListJson("0", msg+"失败!", null);
		}
    }
    
    /**
     * 供应商新增修改保存
     * @param tSupplier
     * @param redirectAttributes
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "supplierSave", method = {RequestMethod.POST,RequestMethod.GET})
    public Object supplierSave(TSupplier tSupplier, TenterpriseLicence2 tEnterpriseLicence, 
    		RedirectAttributes redirectAttributes, HttpServletRequest request){
    	String msg = "";
    	try {
    		if(tSupplier.getId()!=null && 0!=tSupplier.getId()){
    			supplierService.editSupplier(tSupplier,tEnterpriseLicence);
    			msg = "供应商修改";
    		} else {
    			supplierService.saveSupplier(tSupplier, tEnterpriseLicence, request);
    			msg = "供应商保存";
    		}
    		return AppUtil.returnMapJson("1", msg+"成功!", null);
		} catch (Exception e) {
			logger.info(msg+"失败："+e.getMessage());
			return AppUtil.returnMapJson("0", msg+"失败!", null);
		}
    }
    
    /**
     * 查看手机号是否被注册
     * @param phone
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "supplierIsPhone", method = {RequestMethod.POST,RequestMethod.GET})
    public Object supplierIsPhone(String phone, HttpServletRequest request){
    	String msg = "手机号查询";
    	try {
            int sList = supplierService.supplierIsPhone(phone, request);
            if(sList > 0){
            	return AppUtil.returnMapJson("1", "该手机号可注册!", null);
            } else {
            	return AppUtil.returnMapJson("0", "该手机号已被注册!", null);
            }
        } catch (Exception e) {
            logger.info(msg+"失败："+e.getMessage());
            return AppUtil.returnMapJson("0", msg+"失败!", null);
        }
    }
    
    /**
     * 供应商信息删除
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "supplierDel", method = {RequestMethod.POST,RequestMethod.GET})
    public Object supplierDel(String ids){
    	String msg = "供应商删除";
        try {
        	String[] enterpriseIdArray = ids.split(",");
            for (String s : enterpriseIdArray) {
            	supplierService.supplierDel(Integer.parseInt(s));
            }
            return AppUtil.returnMapJson("1", msg+"成功!", null);
        } catch (Exception e) {
            logger.info(msg+"失败："+e.getMessage());
            return AppUtil.returnMapJson("0", msg+"失败!", null);
        }
    }
    
    /**
	 * 图片上传
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "uploadImage", method = RequestMethod.POST)
	public Object uploadImage(HttpServletRequest request) throws Exception {
		try{
			MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
			MultipartFile mFile = mRequest.getFile("img");
			Map<String, Object> fileNames = supplierService.compressUploadedFile(mFile);
			return AppUtil.returnMapJson("1", "上传成功!", fileNames);
		} catch (Exception e) {
			logger.info("上传错误:"+e.getMessage());
			return AppUtil.returnMapJson("0", "上传失败!", null);
		}
	}
}
